import utils.CommonUtil
#下线服务
def offlineService(nacosServer,serviceName,publish,port,namespace,groupName):
    utils.CommonUtil.execCmd("curl --location --request PUT 'http://"+nacosServer+":8848/nacos/v1/ns/instance' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'serviceName="+serviceName+"' \
--data-urlencode 'ip="+publish+"' \
--data-urlencode 'port="+port+"' \
--data-urlencode 'namespaceId="+namespace+"' \
--data-urlencode 'groupName="+groupName+"' \
--data-urlencode 'metadata=preserved.register.source=SPRING_CLOUD' \
--data-urlencode 'enabled=false'")
#上线服务
def onlineService(nacosServer,serviceName,publish,port,namespace,groupName):
    utils.CommonUtil.execCmd("curl --location --request PUT 'http://"+nacosServer+":8848/nacos/v1/ns/instance' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'serviceName="+serviceName+"' \
--data-urlencode 'ip="+publish+"' \
--data-urlencode 'port="+port+"' \
--data-urlencode 'namespaceId="+namespace+"' \
--data-urlencode 'groupName="+groupName+"' \
--data-urlencode 'metadata=preserved.register.source=SPRING_CLOUD' \
--data-urlencode 'enabled=true'")
#取消注册
def deregisterService(nacosServer,serviceName,publish,port,namespace,groupName):
    utils.CommonUtil.execCmd("curl --location --request DELETE 'http://"+nacosServer+":8848/nacos/v1/ns/instance' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'serviceName="+serviceName+"' \
--data-urlencode 'ip="+publish+"' \
--data-urlencode 'port="+port+"' \
--data-urlencode 'namespaceId="+namespace+"' \
--data-urlencode 'groupName="+groupName+"' \
--data-urlencode 'metadata=preserved.register.source=SPRING_CLOUD' \
--data-urlencode 'clusterName=DEFAULT' \
--data-urlencode 'ephemeral=true'")
#获取进程数
def processNum(nacosServer,serviceName,publish,port,namespace,groupName):
    processNumCmd="curl -s --location --request GET 'http://"+nacosServer+":8848/nacos/v1/ns/catalog/instances?serviceName="+serviceName+"&clusterName=DEFAULT&groupName="+groupName+"&pageSize=100&pageNo=1&namespaceId="+namespace+"' | "\
"jq .list[] | jq -r '(.ip|tostring)+\":\"+(.port|tostring)+\":\"+(.enabled|tostring)+\":\"+(.healthy|tostring)' | "\
"grep "+publish+":"+port+":true:true | wc -l"
    # print(processNumCmd)
    return utils.CommonUtil.execCmd(processNumCmd)
